package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.zjitc.entity.*;
import net.zjitc.mapper.*;
import net.zjitc.service.ICompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @auther 郭梦婷
 * @create 2022-04-27 16:12:28
 * @describe 服务实现类
 */
@Service
@Transactional
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private WorkplaceMapper workplaceMapper;
    @Autowired
    private ApplyMapper applyMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MycompanyMapper mycompanyMapper;
    @Autowired
    private RecruitMapper recruitMapper;
    @Autowired
    private MycollectMapper mycollectMapper;


    @Override
    public Page<Company> getCompanys(String querystring, Integer pagenum, Integer pagesize) {
        Page<Company> page=new Page<>(pagenum,pagesize);
        if (querystring.length()==0){
            page = companyMapper.selectPage(page,null);
        }else {
            QueryWrapper<Company> wrapper=new QueryWrapper<>();
            wrapper.like("companyid",querystring).or().like("companyname",querystring);
            page = companyMapper.selectPage(page, wrapper);
        }
        for (Company company: page.getRecords()) {
            if (company.getPlaceid()!=null){
                Workplace workplace = workplaceMapper.selectById(company.getPlaceid());
                company.setPlacename(workplace.getPlacename());
            }

        }
        return page;
    }

    @Override
    public int deleteById(int id) {
        Company company = companyMapper.selectById(id);
        //删除应聘信息
        QueryWrapper<Apply> wrapper1=new QueryWrapper<>();
        wrapper1.eq("comid",wrapper1);
        applyMapper.delete(wrapper1);
        //删除用户
        QueryWrapper<User> wrapper2=new QueryWrapper<>();
        wrapper2.eq("username",company.getCompanyid());
        userMapper.delete(wrapper2);
        //删除我关注的公司
        QueryWrapper<Mycompany> wrapper3=new QueryWrapper<>();
        wrapper3.eq("companyid",company.getCompanyid());
        mycompanyMapper.delete(wrapper3);
        //删除应聘信息
        QueryWrapper<Recruit> wrapper4=new QueryWrapper<>();

        wrapper4.eq("comid",id);
        List<Recruit> recruits = recruitMapper.selectList(wrapper4);
        //删除我关注的岗位
        for (Recruit recruit:recruits) {
            QueryWrapper<Mycollect> wrapper5=new QueryWrapper<>();
            wrapper5.eq("recruitid",recruit.getId());
            mycollectMapper.delete(wrapper5);
        }
        recruitMapper.delete(wrapper4);



        int i = companyMapper.deleteById(id);
        return i;
    }

    @Override
    public Company selectByCompanyid(String companyid) {
        QueryWrapper<Company> wrapper=new QueryWrapper<>();
        wrapper.eq("companyid",companyid);
        Company company = companyMapper.selectOne(wrapper);
        return company;
    }

    @Override
    public Page<Company> SearchCompany(String querystring, String placeids, Integer pagenum, Integer pagesize) {
        Page<Company> page=new Page<>(pagenum,pagesize);
        Integer placeid=0;
        String query=null;
        if (placeids.length()!=0){
            placeid = Integer.parseInt(placeids.split(",")[2]);
        }

        page=companyMapper.SearchCompany(page,querystring,placeid);
        return page;
    }

    @Override
    public Company getCompanyById(Integer id) {
        Company company = companyMapper.selectById(id);
        Workplace workplace = workplaceMapper.selectById(company.getPlaceid());
        company.setPlacename(workplace.getPlacename());
        return company;
    }


}
